{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Astronomical widget libraries\n",
    "\n",
    "The libraries demonstrated here are not as mature as the ones we've seen so far. Keep an eye on them for future developments!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PyWWT - widget interface to the World Wide Telescope \n",
    "\n",
    "### https://github.com/WorldWideTelescope/pywwt\n",
    "\n",
    "World Wide Telescope (WWT) was developed by Microsoft for displaying images of the sky in a variety of projects and several layers; it is like `leaflet` for the sky. Now maintained by the American Astronomical Society (AAS), it now has a widget interface.\n",
    "\n",
    "A javascript API has been available for WWT for a while. The PyWWT package includes javascript to call that API with front ends for both ipywidgets and qt.\n",
    "\n",
    "### Installation\n",
    "\n",
    "`pywwt` is on PyPI and on the `wwt` conda channel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6b6e4316cd5841a08f96344c62212e7f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "WWTJupyterWidget(layout=Layout(align_self='stretch', height='480px'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pywwt.jupyter import WWTJupyterWidget\n",
    "\n",
    "wwt = WWTJupyterWidget()\n",
    "wwt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Several properties of the display can eclipsing binary changed from Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.constellation_figures = True\n",
    "\n",
    "wwt.constellation_boundary_color = 'azure'\n",
    "wwt.constellation_figure_color = '#D3BC8D'\n",
    "wwt.constellation_selection_color = (1, 0, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to interacting with the display with mouse/keyboard, you can manipulate it programmatically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from astropy import units as u\n",
    "from astropy.coordinates import SkyCoord\n",
    "\n",
    "orion_neb = SkyCoord.from_name('Orion Nebula')\n",
    "wwt.center_on_coordinates(orion_neb, fov=10 * u.degree, instant=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A variety of markers can be added to the display, and one can construct tours of the sky."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.load_tour('http://www.worldwidetelescope.org/docs/wtml/tourone.wtt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.pause_tour()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ipyaladdin - interactive sky atlas backed by simbad/vizier databases\n",
    "\n",
    "### https://github.com/cds-astro/ipyaladin\n",
    "\n",
    "The [Simbad catlog]() and [VizieR database interface]() serve as respositories for most public astronomical data. The Aladin sky atlas, originally developed as a desktop application, then an in-browser javascipt app, now has an experimental widget interface.\n",
    "\n",
    "### Installation\n",
    "\n",
    "Installation instructions are at: https://github.com/cds-astro/ipyaladin#installation "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipyaladin.aladin_widget as ipyal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8995c5d365ef48a6804d28f5d9e47e2d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Aladin(fov=10.0, options=['allow_full_zoomout', 'coo_frame', 'fov', 'full_screen', 'log', 'overlay_survey', 'o…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "aladin = ipyal.Aladin(target='Orion Nebula', fov=10, survey='P/allWISE/color')\n",
    "aladin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add markers for items in a data table "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "from astroquery.simbad import Simbad\n",
    "table = Simbad.query_region('Orion Nebula', radius=1 * u.degree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "display_obj = np.random.choice(range(len(table)), size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "aladin.add_table(table[display_obj])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display a local image\n",
    "\n",
    "One goal this week is to wrap the widget below (which displays images stored in a format called FITS that is widely used in astronomy) up in a easily installable widgets. The widget will be demoed during the tutorial but is not yet installable. Code will be in https://github.com/eteq/astrowidgets"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "widgets-tutorial",
   "language": "python",
   "name": "widgets-tutorial"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
